using System;
using System.Configuration;
using Common.Unity.Configuration;
using Common.Unity.Extensions.Configuration;
using Common.Unity.Extensions.PropertyMapper.Policies;
using Microsoft.Practices.ObjectBuilder2;
using Microsoft.Practices.Unity;

namespace Common.Unity.Extensions.PropertyMapper
{
	class PropertyMappingPolicyReferenceElement : PolicyReferenceElement
	{
		public Type SourceType
		{
			get { return TypeResolver.ResolveType( SourceTypeName ); }
		}

		[ConfigurationProperty( SourceTypeNameName, DefaultValue = null, IsRequired = true, IsKey = false )]
		public string SourceTypeName
		{
			get { return (string)this[SourceTypeNameName]; }
			set { this[SourceTypeNameName] = value; }
		}	const string SourceTypeNameName = "sourceType";

		public override InjectionMember CreateInjectionMember()
		{
			IPropertyMappingPolicy policy = (IPropertyMappingPolicy)CreatePolicy();
			return new PropertyMappingPolicyInjection( SourceType, policy );
		}
	}
}